package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());

        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {

        StrategyComment strategyComment = strategyCommentRepository.findById(id).orElse(null);
        return strategyComment;
    }

    @Override
    public List<StrategyComment> listAll() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        //创建查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        Page<StrategyComment> page = DBHelper.query(template, StrategyComment.class, query, qo.getPageable());
        return page;
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //获取评论对象
        StrategyComment strategyComment = this.get(cid);
        //获取评论对象中用户id集合
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断thumbuplist集合中是否包含用户id
        if (!thumbuplist.contains(uid)){
            //如果没有用户id,设置点赞数+1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }else {
            //如果没有用户id,设置点赞数-1
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            //将thumbuplist集合中的当前登录用户id删除
            thumbuplist.remove(uid);
        }
        //更新数据
        strategyCommentRepository.save(strategyComment);
    }


}
